package cn.xianshu.user.controller;

import cn.xianshu.basic.exception.BasicException;
import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.user.dto.LoginDto;
import cn.xianshu.user.service.ILonginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILonginService longinService;

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AxiosResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = longinService.account(loginDto); //  redis  token: loginInfo
            return AxiosResult.me().setResultObj(map); // 把map返回给浏览器端
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setResultObj(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常");
        }
    }

    @PostMapping("/wechat")
     public AxiosResult wechat(@RequestBody Map<String, String> params){
         try {
             return longinService.wechat(params);  // 直接返回获取的数据
         } catch (BasicException e) {
             e.printStackTrace();
             return AxiosResult.me().setMessage(e.getMessage());
         } catch (Exception e) {
             e.printStackTrace();
             return AxiosResult.me().setMessage("系统异常");
         }
     }

    @PostMapping("/binder/wechat")
    public AxiosResult binder(@RequestBody Map<String, String> params){
        try {
            Map<String, Object> map = longinService.binder(params);// 直接返回获取的数据
            return AxiosResult.me().setResultObj(map);
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常");
        }
    }



}
